import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from "@/views/user/Login";
import Register from "@/views/user/Register";
import Forget from "@/views/user/Forget";
import TCourseInfo from "@/views/teacher/TCourseInfo";
import SCourseInfo from "@/views/student/SCourseInfo";
import {Get} from "@/plugins/axios";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '登录',
    component: Login
  },
  {
    path: '/register',
    name: '注册',
    component: Register
  },
  {
    path: '/forget',
    name: '忘记密码',
    component: Forget
  },
  {
    path: '/student',
    children: [
      {
        path: 'courseInfo',
        name: '选课信息',
        component: SCourseInfo
      }
    ]
  },
  {
    path: '/teacher',
    children: [
      {
        path: 'courseInfo',
        name: '任课信息',
        component: TCourseInfo
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

/* 管理系统守卫 */
const _this = this
router.beforeEach((to,from,next) => {
  if(to.path.includes("/teacher")) {
    Get('/teacher/authorization', {}, sessionStorage.getItem('token'))
        .then(res => {
          if(res.data.code === 200) {
            console.log('success')
            next()
          } else {
            console.log('权限不足')
            router.push({ name: '登录'})
          }
        })
  }
  if(to.path.includes("/student")) {
    Get('/student/authorization', {}, sessionStorage.getItem('token'))
        .then(res => {
          if(res.data.code === 200) {
            console.log('success')
            next()
          } else {
            console.log('权限不足')
            router.push({ name: '登录'})
          }
        })
  }
  next()
})

export default router
